
% fig 1cd 2g s2a orientation selectivity exmaple cell 
plotind=find(osi>0.6 & osi_pval<0.05);
plotind=[plotind;124;30;261;229];
% plotind=30;%;
trackcm=150;
close all
% subplot_vec=[6,2,4,8]; 
%========================================each direction
% for i=1:length(plotind)
%     f6=figure('position',[ 363    77   501   500],'color','w');
%     nhd=length(dsiall(plotind(i)).plf_hd);
%     normalizedBy=max(cell2mat(cat(1,dsiall(plotind(i)).plf_hd{:})));
%     maxbin=max(cellfun(@length,cat(1,dsiall(plotind(i)).plf_hd{:})));
%     
%     for j=1:nhd
%         plf=dsiall(plotind(i)).plf_hd{j};
%         [tr1,ia]=sort(dsiall(plotind(i)).hd_track{j});
%         plf=plf(ia);
%         plf=cellfun(@(x) interp1(1:length(x),x,linspace(1,length(x),maxbin)),plf,...
%             'UniformOutput',false);
%         ax= subplot(2,2,j);
%         xdata=linspace(0,trackcm,maxbin);
%         plot_plfield(plf,0,'r',xdata,1,normalizedBy);
%         %         plot_plfield(plf,0,'r',[],1,'self');
%         set(ax,'ytick',1:length(plf),'yticklabel',num2strcell(tr1),...
%             'fontsize',8,'ylim',[1,length(plf)+1],'xlim',[0,trackcm]);
%         %         ax.XAxis.Visible='off';
%         ylabel('Track');xlabel('Location');
%         title(sprintf('%d\\circ',dsiall(plotind(i)).hdvalue(j)));
%         
%         %         if j==1
%         %             ydic=ax.Position(4)/length(plf);
%         %         else
%         %             ax.Position(4)=ydic*length(plf);
%         %         end
%         
%     end
%     %     ax1=axes('Position',[.95,.2,0,ydic],'xtick',[],'ytick',[],...
%     %         'linewidth',2);
%     %     xlabel('50 cm');
%     ylabel(sprintf('%.1f hz',normalizedBy));
%     
%     ht=suptitle(sprintf('%s\nOSI=%.3f,P=%.1g,|DSIeach|=%.2g\nDSI=%.2f,p=%.1g',['rat',num2str(dsiall(plotind(i)).irat),...
%         'cell',num2str(dsiall(plotind(i)).cellid)],dsiall(plotind(i)).osi,...
%         dsiall(plotind(i)).osi_pval,dsiall(plotind(i)).dsi_eachtrack_mean,dsiall(plotind(i)).dsi,...
%         dsiall(plotind(i)).dsi_pval));
%     set(ht,'FontSize',10)
%     setaxisformalAll(10)
%     %     savepdf(gcf,fullfile(savepath,'ExampleCell',['rat',num2str(dsiall(plotind(i)).irat),...
%     %         'cell',num2str(dsiall(plotind(i)).cellid),'.jpg']));
%     %         close(gcf)
% end
%============================overlap of 3 neurons
plotind3=[269,229,238];
f2=figure('color','w','position',[130   317   532   507]);
topdownpeak={'top','down'};ygridsize=0.7;h=[];

for i=1:length(plotind3)
    nhd=length(dsiall(plotind3(i)).plf_hd);
    normalizedBy=max(cell2mat(cat(1,dsiall(plotind3(i)).plf_hd{:})));
    maxbin=max(cellfun(@length,cat(1,dsiall(plotind3(i)).plf_hd{:})));
    
    hdvalue=dsiall(plotind3(i)).hdvalue;
    hdtrack=dsiall(plotind3(i)).hd_track;
    plf=dsiall(plotind3(i)).plf_hd;
    for j=1:length(plf)
        for j1=1:length(plf{j})
            tmp=length(plf{j}{j1});
            plf{j}{j1}=interp1(1:tmp,plf{j}{j1},linspace(1,tmp,maxbin));
        end
    end
    plf1=cat(1,plf{:});
    hd1=[hdtrack{1}',hdvalue(1)*ones(length(hdtrack{1}),1);
        hdtrack{2}',hdvalue(2)*ones(length(hdtrack{2}),1);
        hdtrack{3}',hdvalue(3)*ones(length(hdtrack{3}),1);
        hdtrack{4}',hdvalue(4)*ones(length(hdtrack{4}),1);];
    [hd1,ia]=sortrows(hd1,[1,2]);
    plf1=plf1(ia);
    xdata=linspace(0,trackcm,maxbin);
       subplot(121);
       ntracki=length(plf1)/2;
      fcolor=rand(1,3);
       for idir=1:2
           h1=plot_plfield(plf1(idir:2:end),0,fcolor,xdata,1,normalizedBy,topdownpeak{idir},ygridsize);
           set(h1,'FaceAlpha',0.5);
       end
       hold on;
       h=[h;h1(1)];
       ylabel('Track id');xlabel('Space (cm)');
       set(gca,'ytick',1:ntracki,'yticklabel',num2strcell(hd1(1:2:end,1)),...
            'fontsize',8,'ylim',[0,ntracki+1],'xlim',[0,trackcm],'tickdir','out');
        ystr={'Horizontal track','Vertical track'};
    for j=1:2
       subplot(2,2,j*2) 
       ia=hd1(:,2)==hdvalue(j)|hd1(:,2)==hdvalue(2+j);
       hd2=hd1(ia,:);
       plf2=plf1(ia);ntracki=length(plf2)/2;
%        topdownpeak={'top','down'};ygridsize=0.5;
       for idir=1:2
           h1=plot_plfield(plf2(idir:2:end),0,fcolor,xdata,1,normalizedBy,topdownpeak{idir},ygridsize);
           set(h1,'FaceAlpha',0.5);
       end
       hold on;
        set(gca,'ytick',1:ntracki,'yticklabel',num2strcell(hd2(1:2:end,1)),...
            'fontsize',8,'ylim',[0,ntracki+1],'xlim',[0,trackcm],'tickdir','out');
        if j==1
           set(gca,'xtick',[]); 
        end
        ylabel(ystr{j});
    end
    
    ylabel(sprintf('%.1f hz',normalizedBy));
    
end
xlabel('Space (cm)');
legend(h,num2strcell([dsiall(plotind3).cellid]),'location','best');
setaxisformalAll(10,0)
% %
%%%%%%%%%%%%==========  compare track 1-15 with track grouped by ori
% % subplot_vec=[6,2,4,8];
% for i=1:length(plotind)
%     f61=figure('position',[ 363    77   501   500],'color','w');
%     nhd=length(dsiall(plotind(i)).plf_hd);
%     normalizedBy=max(cell2mat(cat(1,dsiall(plotind(i)).plf_hd{:})));
%     maxbin=max(cellfun(@length,cat(1,dsiall(plotind(i)).plf_hd{:})));
%     
%     hdvalue=dsiall(plotind(i)).hdvalue;
%     hdtrack=dsiall(plotind(i)).hd_track;
%     plf=dsiall(plotind(i)).plf_hd;
%     for j=1:length(plf)
%         for j1=1:length(plf{j})
%             tmp=length(plf{j}{j1});
%             plf{j}{j1}=interp1(1:tmp,plf{j}{j1},linspace(1,tmp,maxbin));
%         end
%     end
%     plf1=cat(1,plf{:});
%     hd1=[hdtrack{1}',hdvalue(1)*ones(length(hdtrack{1}),1);
%         hdtrack{2}',hdvalue(2)*ones(length(hdtrack{2}),1);
%         hdtrack{3}',hdvalue(3)*ones(length(hdtrack{3}),1);
%         hdtrack{4}',hdvalue(4)*ones(length(hdtrack{4}),1);];
%     [hd1,ia]=sortrows(hd1,[1,2]);
%     plf1=plf1(ia);
%     ax=subplot(1,2,1);
%     xdata=linspace(0,trackcm,maxbin);
%     h1=plot_plfield(plf1,0,'r',xdata,1,normalizedBy);
%     for j=1:length(h1)/2
%         set(h1(j*2),'facecolor','m');
%     end
%     set(ax,'ytick',1:2:length(plf1),'yticklabel',num2strcell(hd1(1:2:end,1)),...
%             'fontsize',8,'ylim',[1,length(plf1)+1],'xlim',[0,trackcm],'tickdir','out');
%     ylabel('Track');
%     ystr={'Horizontal track','Vertical track'};
%     for j=1:2
%        subplot(2,2,j*2) 
%        ia=hd1(:,2)==hdvalue(j)|hd1(:,2)==hdvalue(2+j);
%        hd2=hd1(ia,:);
%        plf2=plf1(ia);
%        h1=plot_plfield(plf2,0,'r',xdata,1,normalizedBy);
%        for j1=1:length(h1)/2
%            set(h1(j1*2),'facecolor','m');
%        end
%         set(gca,'ytick',1:2:length(plf2),'yticklabel',num2strcell(hd2(1:2:end,1)),...
%             'fontsize',8,'ylim',[1,length(plf2)+1],'xlim',[0,trackcm],'tickdir','out');
%         if j==1
%            set(gca,'xtick',[]); 
%         end
%         ylabel(ystr{j});
%     end
%     
%     ylabel(sprintf('%.1f hz',normalizedBy));
%     
%     ht=suptitle(sprintf('%s\nOSI=%.3f,P=%.1g,|DSIeach|=%.2g\nDSI=%.2f,p=%.1g',['rat',num2str(dsiall(plotind(i)).irat),...
%         'cell',num2str(dsiall(plotind(i)).cellid)],dsiall(plotind(i)).osi,...
%         dsiall(plotind(i)).osi_pval,dsiall(plotind(i)).dsi_eachtrack_mean,dsiall(plotind(i)).dsi,...
%         dsiall(plotind(i)).dsi_pval));
%     set(ht,'FontSize',10)
%     setaxisformalAll(10,0)
%     %     savepdf(gcf,fullfile(savepath,'ExampleCell',['rat',num2str(dsiall(plotind(i)).irat),...
%     %         'cell',num2str(dsiall(plotind(i)).cellid),'.jpg']));
%     %         close(gcf)
% end
 
%%==============================orientaion==========================%%%%%%%%%%%
topdownpeak={'top','down'};ygridsize=0.7;
for i=1:length(plotind)
    f61=figure('position',[ 363    77   501   500],'color','w');
    nhd=length(dsiall(plotind(i)).plf_hd);
    normalizedBy=max(cell2mat(cat(1,dsiall(plotind(i)).plf_hd{:})));
    maxbin=max(cellfun(@length,cat(1,dsiall(plotind(i)).plf_hd{:})));
    
    hdvalue=dsiall(plotind(i)).hdvalue;
    hdtrack=dsiall(plotind(i)).hd_track;
    plf=dsiall(plotind(i)).plf_hd;
    for j=1:length(plf)
        for j1=1:length(plf{j})
            tmp=length(plf{j}{j1});
            plf{j}{j1}=interp1(1:tmp,plf{j}{j1},linspace(1,tmp,maxbin));
        end
    end
    plf1=cat(1,plf{:});
    hd1=[hdtrack{1}',hdvalue(1)*ones(length(hdtrack{1}),1);
        hdtrack{2}',hdvalue(2)*ones(length(hdtrack{2}),1);
        hdtrack{3}',hdvalue(3)*ones(length(hdtrack{3}),1);
        hdtrack{4}',hdvalue(4)*ones(length(hdtrack{4}),1);];
    [hd1,ia]=sortrows(hd1,[1,2]);
    plf1=plf1(ia);
    ax=subplot(1,2,1);
    xdata=linspace(0,trackcm,maxbin);
       
       ntracki=length(plf1)/2;
      
       for idir=1:2
           h1=plot_plfield(plf1(idir:2:end),0,'r',xdata,1,normalizedBy,topdownpeak{idir},ygridsize);
       end
        set(gca,'ytick',1:ntracki,'yticklabel',num2strcell(hd1(1:2:end,1)),...
            'fontsize',8,'ylim',[0,ntracki+1],'xlim',[0,trackcm],'tickdir','out');
        
        
    ylabel('Track');
    ystr={'Vertical track','Horizontal track'};
    for j=1:2
       subplot(2,2,j*2) 
       ia=hd1(:,2)==hdvalue(j)|hd1(:,2)==hdvalue(2+j);
       hd2=hd1(ia,:);
       plf2=plf1(ia);ntracki=length(plf2)/2;
%        topdownpeak={'top','down'};ygridsize=0.5;
       for idir=1:2
           h1=plot_plfield(plf2(idir:2:end),0,'r',xdata,1,normalizedBy,topdownpeak{idir},ygridsize);
       end
        set(gca,'ytick',1:ntracki,'yticklabel',num2strcell(hd2(1:2:end,1)),...
            'fontsize',8,'ylim',[0,ntracki+1],'xlim',[0,trackcm],'tickdir','out');
        if j==1
           set(gca,'xtick',[]); 
        end
        ylabel(ystr{j});
    end
    
    ylabel(sprintf('%.1f hz',normalizedBy));
    
    ht=suptitle(sprintf('%s\nOSI=%.3f,P=%.1g,|DSIeach|=%.2g\nDSI=%.2f,p=%.1g',['rat',num2str(dsiall(plotind(i)).irat),...
        'cell',num2str(dsiall(plotind(i)).cellid)],dsiall(plotind(i)).osi,...
        dsiall(plotind(i)).osi_pval,dsiall(plotind(i)).dsi_eachtrack_mean,dsiall(plotind(i)).dsi,...
        dsiall(plotind(i)).dsi_pval));
    set(ht,'FontSize',10)
    setaxisformalAll(10,0)

end
saveallfig2pdf(fullfile(savepath,'OSI_example_neuron.pdf'));
%% fig s1g. spike cluster quality

load('/media/Big1/15tracks/spikeClusterQuality.mat');

figure('color','w');
subplot(121);
boxplot(isodis(:,1),isodis(:,2),'Symbol','');
ylabel('Isolation distance');xlabel('Animal');
xlim([0,6]);ylim([0,30]);
title(sprintf('%.4g+-%.4g',isodism,isodisse));
subplot(122);
boxplot(lratio(:,1),lratio(:,2),'Symbol','');
ylabel('Lratio');xlabel('Animal');
xlim([0,6]);ylim([0,3]);
title(sprintf('%.4g+-%.4g',lrm,lrse));
setaxisformalAll;

% savepdf(gcf,'/media/Big1/15tracks/figures/spikeclusterquality.pdf');